package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.BoardInfo;
import model.Command;
import model.IAction;
import model.MyObj;
import model.Position;
import service.GlobalService;
import utilities.CRes;
import utilities.GameResource;
import utilities.TField;
import font.BitmapFont;

public class BoardListScr extends Screen {
	static BoardListScr instance;

    public static BoardListScr gI() {
        if (instance == null) {
            instance = new BoardListScr();
        }
        return instance;
    }
    Command cmdClose;

    private Vector m_items = null;
    public Screen backScreen;

	public byte m_iRoomID;
	
	public BoardListScr(){
		m_cmdLeft = new Command(GameResource.menu, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                Vector menu = new Vector();

                menu.addElement(new Command("Cập nhật", new IAction() {
        			public void perform() {
        				// TODO Auto-generated method stub
        				GameCanvas.startOKDlg(GameResource.plzWait);
        				GlobalService.gI().requestBoardList(m_iRoomID);
        			}
        		}));
                
                menu.addElement(m_viewMsgCmd);
                
                GameCanvas.menu.startAt(menu, 0);
            }
    	});
		
		
		
    	
    	m_cmdCenter = new Command(GameResource.select, new IAction() {					
			public void perform() {
				// TODO Auto-generated method stub
				final BoardInfo r = (BoardInfo)BoardListScr.gI().getSelectedItem();
				if(r.isPass){
					GameCanvas.inputDlg.setInfo("Mật khẩu", new IAction() {
						public void perform() {
							GameCanvas.startWaitDlg(GameResource.plzWait);
							GlobalService.gI().joinBoard(m_iRoomID, r.m_iId, GameCanvas.inputDlg.tfInput.getText());
							GameCanvas.endDlg();
						}
					}, TField.INPUT_TYPE_ANY);
					GameCanvas.inputDlg.show();
				}
				else
				{
					GameCanvas.startWaitDlg(GameResource.plzWait);
					GlobalService.gI().joinBoard(m_iRoomID, r.m_iId, "");
				}
			}
		});
    	
    	m_cmdRight = new Command(GameResource.exit, new IAction() {			
			public void perform() {
				// TODO Auto-generated method stub
				close();
			}
		});
	}
	 
	public void setSelected(int se){
    	if(se != m_selected)
    		super.setSelected(se);
    	else
    		m_cmdCenter.action.perform();
	}
	 
    public void setListItem(Vector items) {
    	clearData();

        m_items = items;
        init();
    }

    public void switchToMe(Command leftCmd, Command centerCmd) {
        m_selected = 0;
        
        m_cmdLeft = null;
        m_cmdLeft = leftCmd;

        m_cmdRight = new Command(GameResource.exit, new IAction() {			
			public void perform() {
				// TODO Auto-generated method stub
				close();
			}
		});
        
        m_cmdCenter = null;
        m_cmdCenter = centerCmd;

        super.switchToMe();
       
    	GameCanvas.endDlg();
    	System.gc();
    }
    public void clearData(){
    	CRes.cleanVector(m_items);
    }
	public void close() {
		// TODO Auto-generated method stub
		GameCanvas.startWaitDlg(GameResource.plzWait);
		GlobalService.gI().requestRoomList();
		
		clearData();
	}
	public void init() {
	    Scroll.gI().init(GameCanvas.h - 50, m_items.size() * 40, CameraList.cmy);
        GameCanvas.cameraList.setInfo(20, 100, GameCanvas.w - 40, 40, GameCanvas.w - 40, m_items.size() * 40,
                GameCanvas.w - 40, (GameCanvas.h - (ITEM_HEIGHT * 3) - 100), m_items.size());
    }
	public void update() {
        Scroll.gI().updateScroll(CameraList.cmy, CameraList.cmtoY);
    }
	
	public void paint(Graphics g) {
		if(m_items == null)
    		return;
    	
        g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		g.setColor(0x3c8dc0);
	    g.fillRect(0, 0, GameCanvas.w, GameCanvas.h);
	    	    
	    PaintPopup.paintRoundRect(g, 10, 10, 126, 30, 10, 0x51659e);
		BitmapFont.drawBoldFont(g, RoomListScr.m_strTitleGame, 73, 25, 0xffffff, Graphics.HCENTER | Graphics.VCENTER);
		
		//paint player info
		int x = GameCanvas.w - 40;
		PaintPopup.paintBox(g, x, 10, 32, 32, 1, 0x51659e, 0x2b6589);
		NuoiThuMidlet.m_myPlayerInfo.m_defaultAvatar.paintIcon(g, x, 10);		
		
		PaintPopup.paintMoney(g, 50, 45, NuoiThuMidlet.m_myPlayerInfo.m_iMoney);
				
		PaintPopup.paintBox(g, 0, 60, GameCanvas.w, 28, 1, 0x9be5ff, 0x9be5ff);
		BitmapFont.drawBoldFont(g, "Khu " + m_iRoomID, GameCanvas.hw, 74, 0x3a001e, Graphics.HCENTER | Graphics.VCENTER);
		GameResource.instance.m_frameIconItems.drawFrame(RoomListScr.m_iIconGame, GameCanvas.hw - 55, 60, 0, g);
						
        paintList(g);
        
        g.translate(-g.getTranslateX(), -g.getTranslateY());
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
        if (m_nMsg > 0) {
            BitmapFont.drawOutlinedString(g, m_nMsg + " ", GameCanvas.w - 20, 15, 0xffff00, 0x000000, Graphics.RIGHT | Graphics.VCENTER);
            g.drawImage(GameResource.instance.imgMesssage, GameCanvas.w - 20, 10, 0);
        }
        
        super.paint(g);
    }

    private void paintList(Graphics g) {
        g.translate(0, ITEM_HEIGHT + 80);
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
        Scroll.gI().paintScroll(g, GameCanvas.w - 10, 20);
        Position pos = GameCanvas.getMinMaxFor(CameraList.cmy, 40, m_items.size(), 1, GameCanvas.h - 60);
        int y = 0;
        y += 40 * pos.x;

        g.setClip(0, 0, GameCanvas.w, GameCanvas.h - 70 - ITEM_HEIGHT + (GameCanvas.w < 150 ? 10 : 0));
        g.translate(0, -CameraList.cmy);

        int i;
        for (i = pos.x; i < pos.y; i++) {
            BoardInfo ava = (BoardInfo) m_items.elementAt(i);
        	ava.m_bIsSelected = (i == m_selected);
            	            
            ava.paintInRow(g, 10, y, GameCanvas.w - 20, 32, i == m_selected);
            y += 40;
        }
        pos = null;
    }

    public MyObj getSelectedItem() {
        if (m_selected >= 0 && m_selected < m_items.size()) {
            return (MyObj) m_items.elementAt(m_selected);
        }
        return null;
    }
    
    public void paintMarqueeMsg(Graphics g){
		PaintPopup.paintBox(g, 0, 0, GameCanvas.w, 20, 1, 0x000000, 0x000000);
    	BitmapFont.drawBoldFont(g, m_strMarqueeMsg, m_iPosXMsg, 10, 0xffffff, Graphics.LEFT | Graphics.VCENTER);
		m_iTimeDisplay++;
		m_iPosXMsg-- ;
		if(m_iPosXMsg <= - m_iStrWidth){
			m_iPosXMsg = GameCanvas.w;
		}
		if(m_iTimeDisplay > 1000){
			m_iTimeDisplay = 0;
			m_strMarqueeMsg = null;
		}
	}
}
